package com.xnyzc.lhy.common.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xnyzc.lhy.common.component.config.security.JwtTokenUtil;
import com.xnyzc.lhy.common.component.config.security.TokenUser;
import com.xnyzc.lhy.common.constant.CommonConstant;
import com.xnyzc.lhy.common.exception.PangException;
import com.xnyzc.lhy.common.penum.EErrorCode;
import com.xnyzc.lhy.order.entity.system.OaSysUser;
import com.xnyzc.lhy.order.entity.user.OaCUser;
import com.xnyzc.lhy.order.entity.user.OaDUser;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 获取user
 *
 * @Description:
 * @Author: scootXin
 * @Date: 2018/12/19 17:47
 */
public class TokenUtil {

    /**
     * 获取clientID
     *
     * @return
     */
    public static String getClientId() {
        HttpServletRequest request = ((ServletRequestAttributes) (RequestContextHolder.currentRequestAttributes())).getRequest();

        return request.getHeader(CommonConstant.REQ_HEADER_CLIENT_ID);
    }

    /**
     * 获取userType
     *
     * @return
     */
    public static String getUserType() {
        HttpServletRequest request = ((ServletRequestAttributes) (RequestContextHolder.currentRequestAttributes())).getRequest();

        return request.getHeader(CommonConstant.REQ_HEADER_USER_TYPE);
    }

    /**
     * 获取tag
     *
     * @return
     */
    public static String getTag() {
        HttpServletRequest request = ((ServletRequestAttributes) (RequestContextHolder.currentRequestAttributes())).getRequest();

        return request.getHeader(CommonConstant.REQ_HEADER_TAG);
    }

    /**
     * 从request中获取到tokenUser
     *
     * @param httpServletRequest
     * @return
     */
    public static TokenUser getTokenUser(HttpServletRequest httpServletRequest) {
        String token = getToken(httpServletRequest);

        return JwtTokenUtil.getUserFromToken(token);
    }

    /**
     * 获取用户id
     *
     * @return
     */
    public static long getTokenUserId() {
        TokenUser tokenUser = getTokenUser();

        if (tokenUser == null) {
            throw PangException.create(EErrorCode.missingAuthArgs);
        }
        String userJson = JSON.toJSONString(tokenUser.getUser());
        JSONObject jsonObject = JSON.parseObject(userJson);
        if (jsonObject.containsKey("driverId")) {
            OaDUser oaDUser = JSON.parseObject(userJson, OaDUser.class);
            return oaDUser.getDriverId();
        } else if (jsonObject.containsKey("userId")){
            OaCUser oaCUser = JSON.parseObject(userJson, OaCUser.class);
            return oaCUser.getUserId();
        } else {
            OaSysUser oaSysUser = JSON.parseObject(userJson, OaSysUser.class);
            return oaSysUser.getSysUserId();
        }
    }

    /**
     * 获取用户实体
     *
     * @return
     */
    public static Object getTokenUserEntity(Class clazz) {
        TokenUser tokenUser = getTokenUser();
        JSONObject user = (JSONObject) tokenUser.getUser();
        Object userEntity = JSONObject.toJavaObject(user, clazz);
        if (tokenUser == null) {
            throw PangException.create(EErrorCode.missingAuthArgs);
        }
        return userEntity;
    }

    /**
     * 获取用户实体(heli)
     *
     * @return
     */
    public static Object getTokenUserEntityHeLi(String token, Class clazz) {
        TokenUser tokenUser = JwtTokenUtil.getUserFromToken(token);

        if (tokenUser == null) {
            throw PangException.create(EErrorCode.userInforExpire);
        }
        JSONObject user = (JSONObject) tokenUser.getUser();
        Object userEntity = JSONObject.toJavaObject(user, clazz);
        if (tokenUser == null) {
            throw PangException.create(EErrorCode.missingAuthArgs);
        }
        return userEntity;
    }


    /**
     * 获取tokenUser
     *
     * @return
     */
    public static TokenUser getTokenUser() {
        HttpServletRequest request = ((ServletRequestAttributes) (RequestContextHolder.currentRequestAttributes())).getRequest();

        String token = getToken(request);

        TokenUser tokenUser = JwtTokenUtil.getUserFromToken(token);

        if (tokenUser == null) {
            throw PangException.create(EErrorCode.userInforExpire);
        }

        return tokenUser;
    }

    /**
     * 获取请求中token
     *
     * @param httpServletRequest request
     * @return token
     */
    public static String getToken(HttpServletRequest httpServletRequest) {
        String authorization = httpServletRequest.getHeader(CommonConstant.REQ_HEADER);

        if (CheckUtil.strIsEmpty(authorization)) {
            throw PangException.create(EErrorCode.missingAuthArgs);
        }

        String token = StringUtils.substringAfter(authorization, CommonConstant.TOKEN_SPLIT);

        if (CheckUtil.strIsEmpty(token)) {
            throw PangException.create(EErrorCode.authErr);
        }

        return token;
    }

    /**
     * 获取用户角色
     *
     * @return
     */
    public static List<String> getRoleUserId() {
        TokenUser tokenUser = getTokenUser();
        if (tokenUser == null) {
            throw PangException.create(EErrorCode.missingAuthArgs);
        }
        return tokenUser.getRoles();
    }

    /**
     * 获取角色数据范围
     *
     * @return
     */
    public static List<String> getRoleDataScope() {
        TokenUser tokenUser = getTokenUser();
        if (tokenUser == null) {
            throw PangException.create(EErrorCode.missingAuthArgs);
        }
        return tokenUser.getDataScope();
    }

}
